package com.ulrica.morning.tools.tcmsp;

import com.alibaba.fastjson2.JSON;
import com.ulrica.morning.dto.tcmsp.DrugName;
import com.ulrica.morning.dto.tcmsp.LatinName;
import lombok.SneakyThrows;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author LongKui
 * @since 2022/06/28
 */
public class GrabLatinName {
    private static final String URI = "https://old.tcmsp-e.com/tcmspsearch.php?qs=herb_all_name&q=%s&token=71c3de2323b5cabf70be678a64c9aff5";
    private static final String START_WITH = "data: ";

    /**
     * 酒精性脂肪肝
     * 葛花、木香、砂仁、茯苓、猪苓、人参、白术、白豆蔻、青皮、陈皮、神曲、干姜、泽泻
     */
    @SneakyThrows
    public static void main(String[] args) {
        String pinyin = "Renshen,Ganjiang,Gehua,Zhuling,Qingpi,Sharen,Zexie,Fuling,Chenpi,Shenqu,Muxiang,Baizhu,Baidoukou";
        Set<String> pinyinSet = new HashSet<>(Arrays.asList(pinyin.split(",")));
        Set<LatinName> latinNameSet = getLatinNameSet(pinyinSet);
        System.out.println("===================");
        latinNameSet.forEach(System.out::println);
    }

    public static Set<LatinName> pinyin2Latin(Set<DrugName> drugNameSet) {
        Set<LatinName> latinSet = new HashSet<>();
        for (DrugName drugName : drugNameSet) {
            Set<LatinName> latinNameSet = getLatinNameSet(drugName.getPinyin());
            latinSet.addAll(latinNameSet);
        }
        return latinSet;
    }


    public static Set<LatinName> getLatinNameSet(Set<String> pinyin) {
        Set<LatinName> latinSet = new HashSet<>();
        for (String name : pinyin) {
            LatinName latinName = getLatinName(name);
            if (Objects.nonNull(latinName)) {
                latinSet.add(latinName);
            }
        }
        return latinSet;
    }

    @SneakyThrows
    private static LatinName getLatinName(String name) {
        Connection connect = Jsoup.connect(String.format(URI, name));
        Document doc = connect.get();
        Elements elements = doc.getElementsByTag("script");

        Element element = elements.get(8);
        String data = element.data();
        String[] gridArr = data.split("\r\n");
        for (String s : gridArr) {
            s = s.trim();
            if (s.startsWith(START_WITH)) {
//                System.out.println(name + "->" + s);
                String content = s.substring(s.indexOf("["), s.length() - 1);
                List<LatinName> list = JSON.parseArray(content, LatinName.class);
                List<LatinName> latinNameList = list.stream().filter(latinName -> name.equals(latinName.getHerb_pinyin())).collect(Collectors.toList());
                return CollectionUtils.isEmpty(latinNameList) ? null : latinNameList.get(0);
            }
        }
        return null;
    }
}
